//
//  Door.m
//  Dolphin
//
//  Created by Matthew on 12-2-8.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "Door.h"


@implementation Door

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code
        upHalfDoor = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 318)];
        [upHalfDoor setImage:[UIImage imageNamed:@"up.jpg"]];
        upHalfDoor.userInteractionEnabled = NO;
        [self addSubview:upHalfDoor];
        
        downHalfDoor = [[UIImageView alloc] initWithFrame:CGRectMake(0, 318, 1024, 450)];
        [downHalfDoor setImage:[UIImage imageNamed:@"down.jpg"]];
        downHalfDoor.userInteractionEnabled = NO;
        [self addSubview:downHalfDoor];
        
        self.userInteractionEnabled = FALSE;
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void) openDoor
{
    [UIView transitionWithView:self duration:1.0f 
                       options:UIViewAnimationCurveEaseOut  
                    animations:^{
                        upHalfDoor.transform = CGAffineTransformMakeTranslation(0, -upHalfDoor.frame.size.height);
                        downHalfDoor.transform = CGAffineTransformMakeTranslation(0, downHalfDoor.frame.size.height);
                    }
                    completion:NULL];
}

-(void) closeDoor
{
    [UIView transitionWithView:self duration:0.5f 
                       options:UIViewAnimationCurveEaseIn
                    animations:^{
                        upHalfDoor.transform = CGAffineTransformMakeTranslation(0, 0);
                        downHalfDoor.transform = CGAffineTransformMakeTranslation(0, 0);
                    }
                    completion:NULL];
}

-(void) closeAndOpen
{
    [UIView transitionWithView:self duration:0.5f 
                       options:UIViewAnimationCurveEaseIn
                    animations:^{
                        upHalfDoor.transform = CGAffineTransformMakeTranslation(0, 0);
                        downHalfDoor.transform = CGAffineTransformMakeTranslation(0, 0);
                    }
                    completion:^(BOOL finished) 
                    {
                        //[self openDoor];
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"SHIFT_TO_PAGE" object:self];    

                         [UIView animateWithDuration:0.5f 
                         delay:0.5f 
                         options:UIViewAnimationCurveEaseOut 
                         animations:^{
                         upHalfDoor.transform = CGAffineTransformMakeTranslation(0, -upHalfDoor.frame.size.height);
                         downHalfDoor.transform = CGAffineTransformMakeTranslation(0, downHalfDoor.frame.size.height);
                         }
                         completion:NULL];

                    }];
}

- (void)dealloc
{
    [super dealloc];
}

@end
